﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using SYcwpb.Models;
using System.Configuration;

namespace SYcwpb.Helpers
{
    public class CONSTDATA
    {
        public const string IP = "127.0.0.1"; // 默认远程ip地址

        public const int Port = 8888;

        public const int Driver_Tips_Interval = 1000 * 15;//半分钟

        public static bool IsNewSetRoom = Convert.ToBoolean(ConfigurationManager.AppSettings["isNewSetRoom"]);

        public static LoginModel UserModel = LoginModel.CreateLoginModel();

        public static int RemindingInterval = Convert.ToInt32(ConfigurationManager.AppSettings["remindingInterval"]) * 1000;

        private static Dictionary<int, int> sortStation2 = null;
        private static Dictionary<int, int> sortStation1 = null;

        /// <summary>
        /// 迟到分等级的时间
        /// </summary>
        public static int DelayInter = 15;

        private static Dictionary<string, int> SationName_To_ID;

        private static Dictionary<int, string> StationID_To_Name;

        public static Dictionary<string,int>InitSationName_To_ID_1()
        {
            SationName_To_ID = new Dictionary<string, int>();

            SationName_To_ID.Add("SSH",1);
            SationName_To_ID.Add("ZYD",2);
            SationName_To_ID.Add("QHJ",3);
            SationName_To_ID.Add("SHJ",4);
            SationName_To_ID.Add("ZSZ",5);
            SationName_To_ID.Add("KFD",6);
            SationName_To_ID.Add("YHG",7);
            SationName_To_ID.Add("YBL",8);
            SationName_To_ID.Add("ZGJ",9);
            SationName_To_ID.Add("QGJ",10);
            SationName_To_ID.Add("BGJ",11);
            SationName_To_ID.Add("TXG",12);
            SationName_To_ID.Add("YFB",13);
            SationName_To_ID.Add("SYZ",14);
            SationName_To_ID.Add("TYJ",15);
            SationName_To_ID.Add("NSC",16);
            SationName_To_ID.Add("QND",17);
            SationName_To_ID.Add("HYM", 18);
            SationName_To_ID.Add("ZJZ", 19);
            SationName_To_ID.Add("DZJ", 20);
            SationName_To_ID.Add("PJJ", 21);
            SationName_To_ID.Add("LMG", 22);

            SationName_To_ID.Add("TLM",30);
            SationName_To_ID.Add("TSH",31);

            SationName_To_ID.Add("WD1",66);
            SationName_To_ID.Add("WD2",67);

            return SationName_To_ID;
        }
        public static Dictionary<int, string> InitStationID_To_Name_1()
        {
            StationID_To_Name = new Dictionary<int, string>();

            StationID_To_Name.Add(1, "十三号街站");
            StationID_To_Name.Add(2, "中央大街站");
            StationID_To_Name.Add(3, "七号街站");
            StationID_To_Name.Add(4, "四号街站");
            StationID_To_Name.Add(5, "张士站");
            StationID_To_Name.Add(6, "开发大道站");
            StationID_To_Name.Add(7, "于洪广场站");
            StationID_To_Name.Add(8, "迎宾路站");
            StationID_To_Name.Add(9, "重工街站");
            StationID_To_Name.Add(10, "启工街站");
            StationID_To_Name.Add(11, "保工街站");
            StationID_To_Name.Add(12, "铁西广场站");
            StationID_To_Name.Add(13, "云峰北街站");
            StationID_To_Name.Add(14, "沈阳站站");
            StationID_To_Name.Add(15, "太原街站");
            StationID_To_Name.Add(16, "南市场站");
            StationID_To_Name.Add(17, "青年大街站");
            StationID_To_Name.Add(18, "怀远门站");
            StationID_To_Name.Add(19, "中街站");
            StationID_To_Name.Add(20, "东中街站");
            StationID_To_Name.Add(21, "滂江街站");
            StationID_To_Name.Add(22, "黎明广场站");

            StationID_To_Name.Add(30, "折返轨TLM");
            StationID_To_Name.Add(31, "折返轨TSH");

            StationID_To_Name.Add(66, "转换轨1");
            StationID_To_Name.Add(67, "转换轨2");

            return StationID_To_Name;
        }
        public static Dictionary<string, int> InitSationName_To_ID_2()
        {
            SationName_To_ID = new Dictionary<string, int>();

            SationName_To_ID.Add("QYL", 1);
            SationName_To_ID.Add("BTH", 2);
            SationName_To_ID.Add("SJD", 3);
            SationName_To_ID.Add("YPJ", 4);
            SationName_To_ID.Add("ATZ", 5);
            SationName_To_ID.Add("WLH", 6);
            SationName_To_ID.Add("STS", 7);
            SationName_To_ID.Add("GYZ", 8);
            SationName_To_ID.Add("QNG", 9);
            SationName_To_ID.Add("QND", 10);
            SationName_To_ID.Add("SFG", 11);
            SationName_To_ID.Add("JRZ", 12);
            SationName_To_ID.Add("SYB", 13);
            SationName_To_ID.Add("QSL", 14);
            SationName_To_ID.Add("ZYY", 15);
            SationName_To_ID.Add("BLG", 16);
            SationName_To_ID.Add("XLY", 17);
            SationName_To_ID.Add("LXZ", 18);
            SationName_To_ID.Add("STZ", 19);
            //2013-12-02添加3个站
            SationName_To_ID.Add("YXY", 20);
            SationName_To_ID.Add("SFD", 21);
            SationName_To_ID.Add("HKH", 22);
            SationName_To_ID.Add("LND", 23);
            SationName_To_ID.Add("RJH", 24);
            
            SationName_To_ID.Add("PHD", 25);
            SationName_To_ID.Add("PTL", 26);

            SationName_To_ID.Add("TST", 30);
            SationName_To_ID.Add("TSG", 31);
            SationName_To_ID.Add("PTT", 32);
            SationName_To_ID.Add("TPT", 33);


            SationName_To_ID.Add("ED1", 66);
            SationName_To_ID.Add("ED2", 67);

            return SationName_To_ID;
        }
        public static Dictionary<int, string> InitStationID_To_Name_2()
        {
            StationID_To_Name = new Dictionary<int, string>();

            StationID_To_Name.Add(1, "全运路站");
            StationID_To_Name.Add(2, "白塔河路站");
            StationID_To_Name.Add(3, "世纪大厦站");
            StationID_To_Name.Add(4, "营盘街站");
            StationID_To_Name.Add(5, "奥体中心站");
            StationID_To_Name.Add(6, "五里河站");
            StationID_To_Name.Add(7, "市图书馆站");
            StationID_To_Name.Add(8, "工业展览馆站");
            StationID_To_Name.Add(9, "青年公园站");
            StationID_To_Name.Add(10, "青年大街站");
            StationID_To_Name.Add(11, "市府广场站");
            StationID_To_Name.Add(12, "金融中心站");
            StationID_To_Name.Add(13, "沈阳北站站");
            StationID_To_Name.Add(14, "岐山路站");
            StationID_To_Name.Add(15, "中医药大学站");
            StationID_To_Name.Add(16, "北陵公园站");
            StationID_To_Name.Add(17, "新乐遗址站");
            StationID_To_Name.Add(18, "陵西站");
            StationID_To_Name.Add(19, "三台子站");
            StationID_To_Name.Add(20, "医学院站");
            StationID_To_Name.Add(21, "师范大学站");
            StationID_To_Name.Add(22, "航空航天大学站");
            StationID_To_Name.Add(23, "辽宁大学站");
            StationID_To_Name.Add(24, "人杰湖公园站");
            
            StationID_To_Name.Add(25, "蒲河大道站");
            StationID_To_Name.Add(26, "蒲田路站");
            StationID_To_Name.Add(30, "折返轨TST");
            StationID_To_Name.Add(31, "折返轨TSG");
            StationID_To_Name.Add(32, "折返轨PTT");
            StationID_To_Name.Add(33, "折返轨TPT");

            StationID_To_Name.Add(66, "转换轨1");
            StationID_To_Name.Add(67, "转换轨2");

            return StationID_To_Name;
        }
        public static Dictionary<int, int> InitStationSort_To_Name_1()
        {
            if (sortStation1 != null)
                return sortStation1;
            sortStation1 = new Dictionary<int, int>();

            sortStation1.Add(1, 3);
            sortStation1.Add(2, 4);
            sortStation1.Add(3, 5);
            sortStation1.Add(4, 6);
            sortStation1.Add(5, 7);
            sortStation1.Add(6, 8);
            sortStation1.Add(7, 9);
            sortStation1.Add(8, 10);
            sortStation1.Add(9, 11);
            sortStation1.Add(10, 12);
            sortStation1.Add(11, 13);
            sortStation1.Add(12, 14);
            sortStation1.Add(13, 15);
            sortStation1.Add(14, 16);
            sortStation1.Add(15, 17);
            sortStation1.Add(16, 18);
            sortStation1.Add(17, 19);
            sortStation1.Add(18, 20);
            sortStation1.Add(19, 21);
            sortStation1.Add(20, 22);
            sortStation1.Add(21, 23);
            sortStation1.Add(22, 24);

            sortStation1.Add(30, 25);
            sortStation1.Add(31, 26);

            sortStation1.Add(66, 1);
            sortStation1.Add(67, 2);

            return sortStation2;
        }
        public static Dictionary<int, int> InitStationSort_To_Name_2()
        {
            if (sortStation2 != null)
                return sortStation2;
            sortStation2 = new Dictionary<int, int>();

            sortStation2.Add(1, 3);
            sortStation2.Add(2, 4);
            sortStation2.Add(3, 5);
            sortStation2.Add(4, 6);
            sortStation2.Add(5, 7);
            sortStation2.Add(6, 8);
            sortStation2.Add(7, 9);
            sortStation2.Add(8, 10);
            sortStation2.Add(9, 11);
            sortStation2.Add(10, 12);
            sortStation2.Add(11, 13);
            sortStation2.Add(12, 14);
            sortStation2.Add(13, 15);
            sortStation2.Add(14, 16);
            sortStation2.Add(15, 17);
            sortStation2.Add(16, 18);
            sortStation2.Add(17, 19);
            sortStation2.Add(18, 20);
            sortStation2.Add(19, 21);
            sortStation2.Add(20, 22);
            sortStation2.Add(21, 23);
            sortStation2.Add(22, 24);
            sortStation2.Add(23, 25);
            sortStation2.Add(24, 26);
            sortStation2.Add(25, 27);
            sortStation2.Add(26, 28);

            sortStation2.Add(30, 22);
            sortStation2.Add(31, 1);
            sortStation2.Add(32, 29);

            sortStation2.Add(66, 1);
            sortStation2.Add(67, 2);

            return sortStation2;
        }



        /*-----------------------------判断文件是否打开------------------------------------*/
        [DllImport("kernel32.dll")]
        public static extern IntPtr _lopen(string lpPathName, int iReadWrite);

        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(IntPtr hObject);

        public static int OF_READWRITE = 2;
        public static int OF_SHARE_DENY_NONE = 0x40;
        public static readonly IntPtr HFILE_ERROR = new IntPtr(-1);

    }

    public static class GlobalVariables
    {
        public static LoginModel loginModel=new LoginModel();
        public static bool IsNumberic(string _string)
        {
            int a = 0;
            return int.TryParse(_string, out a);
        }

        public static bool IsDouble(string _string)
        {
            double a = 0;
            return double.TryParse(_string, out a);
        }

        public static void GetLineTwoTrain(string tbname, ref ArrayList TrainIdofTwo)
        {
            string sql = "select distinct MidID from [" + tbname + "] order by MidID ASC";
            try
            {
                TrainIdofTwo.Clear();
                SqlDataLayer traindivide = new SqlDataLayer();
                DataTable dtLineTwo = traindivide.SelectDt(sql);
                if (dtLineTwo != null)
                {
                    for (int i = 0; i < dtLineTwo.Rows.Count; i++)
                    {
                        TrainIdofTwo.Add(dtLineTwo.Rows[i]["MidID"].ToString());
                    }
                }

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        public static string LoadHeadImage(string user_en)
        {
            //string fold = "HeadImages";
            string imageRoot = System.Windows.Forms.Application.StartupPath;

            string imagePath = imageRoot + @"\headImages\" + user_en + ".jpg";
            string defaultPath = imageRoot + @"\Images\defaultHeadImage.jpg";

            if (!Directory.Exists(imageRoot + @"\headImages\"))
            {
                Directory.CreateDirectory(imageRoot + @"\headImages\");
            }

            if (!File.Exists(imagePath))
                if (!File.Exists(defaultPath))
                    return null;
                else
                    return defaultPath;

            return imagePath;
        }

        public static StringBuilder DecryptCode(string code)
        {
            MD5 md5pwd = MD5.Create();
            byte[] criptdata = md5pwd.ComputeHash(Encoding.Default.GetBytes(code));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < criptdata.Length; i++)
            {
                sb.Append(criptdata[i].ToString("x2"));
            }
            return sb;
        }

        /// <summary>
        /// 将秒以hh:mm:ss的形式表示
        /// </summary>
        /// <param name="timestr"></param>
        /// <returns></returns>
        public static string FormatTime(string timestr)
        {
            if (timestr.Length < 1) { return null; }
           
            int getobj = Convert.ToInt32(timestr);
            return FormatTime(getobj);
        }
        public static string FormatTime(int getobj)
        {

            if (getobj > 86400)
            {
                getobj -= 86400;
            }
            long a, b, c;
            string ret = string.Empty;
            if (getobj > 0)
            {
                a = (getobj / 3600);
                b = ((getobj % 3600) / 60);
                c = ((getobj % 3600) % 60);
                ret = a < 10 ? "0" + a.ToString() : a.ToString();
                ret += ":";
                ret += b < 10 ? "0" + b.ToString() : b.ToString();
                ret += ":";
                ret += c < 10 ? "0" + c.ToString() : c.ToString();
            }
            else
            {
                ret = "00:00:00";
            }
            return ret;
        }
        /// <summary>
        /// 将秒以hh:mm:ss的形式表示,其中秒设置为00，仅司机公寓安排中使用
        /// </summary>
        /// <param name="timestr"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public static string FormatTime(string timestr,bool flag)
        {
                if (timestr.Length < 1) { return null; }

                int getobj = Convert.ToInt32(timestr);
                if (getobj > 86400)
                {
                    getobj -= 86400;
                }
                long a, b, c;
                string ret = string.Empty;
                if (getobj > 0)
                {
                    a = (getobj / 3600);
                    b = ((getobj % 3600) / 60);
                    c = ((getobj % 3600) % 60);
                    ret = a < 10 ? "0" + a.ToString() : a.ToString();
                    ret += ":";
                    ret += b < 10 ? "0" + b.ToString() : b.ToString();
                    ret += ":";
                    ret += "00";
                }
                else
                {
                    ret = "00:00:00";
                }
                return ret;
        }
        /// <summary>
        /// 将hh:mm:ss的形式以秒返回
        /// </summary>
        /// <param name="strTime"></param>
        /// <returns></returns>
        public static int FormatTimeToInt(string strTime)
        {
            if (strTime == null || strTime.Length < 1) { return 0; }
            string[] timeArr = strTime.Split(':');
            int hour = Convert.ToInt32(timeArr[0]);
            int mm = Convert.ToInt32(timeArr[1]);
            int ss = Convert.ToInt32(timeArr[2]);

            return hour * 3600 + mm * 60 + ss;
        }

        public static int FormatTimeToInt(DateTime time)
        {
            return time.Hour * 3600 + time.Minute * 60 + time.Second;
        }

        /// <summary>
        /// 获取某个车次的发车时间
        /// </summary>
        /// <param name="tbName">时刻表名字</param>
        /// <param name="trainId">车次号</param>
        /// <param name="timeFormat">返回的时间格式：0为秒，1为HH:mm:ss</param>
        /// <returns></returns>
        public static object GetStartTime(string tbName,string trainId,int timeFormat)
        {
            SqlDataLayer sqlHelper = new SqlDataLayer();
            string strSqlStart = "select top 1 Depart from [" + tbName + "] where TrainId = '" + trainId + "' and Depart<>-1 order by Depart asc";
            if (timeFormat == 0)
            {
                return sqlHelper.GetSingle(strSqlStart);
            }
            else { return GlobalVariables.FormatTime(Convert.ToString(sqlHelper.GetSingle(strSqlStart))); }
            
        }

        /// <summary>
        /// 获取某车次于某站的到站时间
        /// </summary>
        /// <param name="tbName">时刻表名字</param>
        /// <param name="trainId">车次号</param>
        /// <param name="timeFormat">返回的时间格式：0为秒，1为HH:mm:ss</param>
        /// <returns></returns>
        public static object GetArriveTimeFromSta(string tbName, string trainId,int StaID, int timeFormat)
        {
            SqlDataLayer sqlHelper = new SqlDataLayer();
            string strSqlStart = "select Arrive from [" + tbName + "] where TrainId = '" + trainId + "' and Station = " + StaID + " and Arrive<>0";
            object result = null;
            if (timeFormat == 0)
            {
                result = sqlHelper.GetSingle(strSqlStart);

                return  result == null ? 0: result ;
            }
            else {
                result = sqlHelper.GetSingle(strSqlStart);

                if (null == result)
                    return 0;
                else
                    return GlobalVariables.FormatTime(Convert.ToString(result)); 
            }

        }

        /// <summary>
        /// 获取某车次于某站的发车时间
        /// </summary>
        /// <param name="tbName">时刻表名字</param>
        /// <param name="trainId">车次号</param>
        /// <param name="timeFormat">返回的时间格式：0为秒，1为HH:mm:ss</param>
        /// <returns></returns>
        public static object GetDepartTimeFromSta(string tbName, string trainId, int StaID, int timeFormat)
        {
            SqlDataLayer sqlHelper = new SqlDataLayer();
            string strSqlStart = "select Depart from [" + tbName + "] where TrainId = '" + trainId + "' and Station = " + StaID + " and Depart<>-1";
            if (timeFormat == 0)
            {
                return sqlHelper.GetSingle(strSqlStart);
            }
            else { return GlobalVariables.FormatTime(Convert.ToString(sqlHelper.GetSingle(strSqlStart))); }

        }


        /// <summary>
        /// 返回提前后的时间
        /// </summary>
        /// <param name="myTime">HH:mm:ss</param>
        /// <param name="minAhead">int min</param>
        /// <returns></returns>
        public static string GetAheadTime(string myTime,int minAhead)
        {
            string result = (FormatTimeToInt(myTime) - minAhead * 60).ToString();
            return GlobalVariables.FormatTime(result);
        }

        /// <summary>
        /// 获取某个车次的结束时间
        /// </summary>
        /// <param name="tbName">时刻表名字</param>
        /// <param name="trainId">车次号</param>
        /// <param name="trainId">车次号</param>
        /// <returns></returns>
        public static object GetFinishTime(string tbName, string trainId,int timeFormat)
        {
            SqlDataLayer sqlHelper = new SqlDataLayer();
            string strSqlFinish = "select top 1 Arrive from [" + tbName + "] where TrainId = '" + trainId + "' order by Arrive desc";

            if (timeFormat == 0)
            {
                return sqlHelper.GetSingle(strSqlFinish);
            }
            else { return GlobalVariables.FormatTime(Convert.ToString(sqlHelper.GetSingle(strSqlFinish))); }
            

        }

    }
}
